<template>
	<view class="uni-container">
		<!-- <uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			title="公租房" @clickLeft="back" /> -->
			<cu-custom bgColor="bg-gradual-blue" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">单身公寓</block>
			</cu-custom>
		<view class="scorll-style">
			<view v-for="(item,index) in tableData" :key="index" class="c-list">
				<uni-card :title="item.name" mode="title" :is-shadow="true" :extra="item.id" note="Tips">
					
					<view v-if="item.id === myApplyInfo.projectId">
						当前进度：
						<uni-steps :options="steplist" active-color="#007AFF" :active="myApplyInfo.step - 1" direction="column" />
					</view>
					<view v-if="item.id === myApplyInfo.projectId">
						申请于: {{myApplyInfo.createTime}}
					</view>
					<view v-if="item.id === myApplyInfo.projectId && myApplyInfo.updateTime">
						更新于: {{myApplyInfo.updateTime}}
					</view>
					
					<view v-if="myTurnInfo.projectid && item.id === myTurnInfo.projectid">
						<view class="tag-view">
							<uni-tag text="轮候中" type="success" />
						</view>
						当前轮候排名: {{myTurnInfo.rank}}
					</view>
					
					<view>
						地址：{{item.province + item.city + item.location}}
					</view>
					<view slot="actions" class="card-actions">
						<button size="mini" class="btn2" @click="signUp(item.id)">申请</button>
					</view>
				</uni-card>
			</view>
		</view>
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog type="info" cancelText="取消" confirmText="确认申请" title="通知" :content="messageText"
					@confirm="applyOnclick()" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'singleHosing',
		data() {
			return {
				steplist: [{
					title: '提交申请',
				},{
					title: '职工单位科室审核',
				}, {
					title: '管理单位科室审核',
				}, {
					title: '管理单位领导审核',
					desc: ''
				}, {
					title: '管理单位公寓车间审核',
				}],
				
				flag: 0,
				tableData: [{
					title: 'xx项目公租房报名',
					project_dictText: '',
					isStopApply_dictText: '',
					isStopCheck_dictText: '',
					isFinish_dictText: '',
					currentStatus_dictText: '',
				}],
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				person: [0, 1, 2, 3],
				msgType: 'success',
				messageText: '这是一条成功提示',
				
				userSingleApplyReminderModel: {
				  canApply: false,
				  applyidx: '',
				},
				
				myApplyInfo:{},
				myTurnInfo:{},
			}
		},
		mounted() {
			this.$http.get('/projectroom/znzfProject/listByTypes',
				{
					params:{
						filterType:"0"
					}
				}
			).then(res => {
				//console.log(1, res.data.result.records)
				this.tableData = res.data.result.records
			})
			
			//获取轮候信息
			this.$http.get('/base/znzfEmployee/queryMyTurn').then(res => {
				res = res.data;
				if(!res.success){
					return;
				}
				else{
					let data = res.result;
					if(!data || data === {}) return;
					this.myTurnInfo = data;
				}
			})
			
			this.$http.post('/single/znzfNeedSingle/searchByToken').then(res => {
				res = res.data;
				if(!res.success){
					return;
				}
				else{
					let data = res.result;
					if(!data || data === {}) return;
					
					this.myApplyInfo = data;
					
					if(data.checkState === 2){
						uni.showModal({
							title: '您的申请已被驳回',
							content: '管理员审核意见' + data.opinion,
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
					else if(data.checkState === 0){
						uni.showModal({
							title: '提示',
							content: '恭喜你，您的申请已通过！',
							success: function (res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				}
			})
			
		},
		methods: {
			applyLiveSingle(e){
				this.userSingleApplyReminderModel = {
				  canApply: true,
				  applyidx: e,
				};
				

				let params ={
					projectId: e,
				}
				this.$http.post('/single/znzfNeedSingle/checkapplyinfo',params).then((res) => {
					res = res.data;
					if (res.success) {
						this.dialogToggle('success', res.result.message.replace(/&/g, "\n"))
					}
					else{
						uni.showToast({
							title: res.message,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			signUp(e) {
				let project = this.tableData.filter(item => item.id == e)[0]
				// console.log(project)
				this.applyLiveSingle(e);
				
			},
			applyOnclick(){
				if(!this.userSingleApplyReminderModel.applyidx){
					uni.showToast({
						title: '提交错误！',
						icon: 'error',
						duration: 2000
					});
					return;
				}
				let params ={
					projectId: this.userSingleApplyReminderModel.applyidx,
				}
				
				this.$http.post('/single/znzfNeedSingle/insert', params).then((res) => {
				  res = res.data;
				  if (res.success) {
					uni.showToast({
						title: '申请成功',
						duration: 2000
					});
				  }
				  else{
					uni.showToast({
						title: res.message,
						icon: 'error',
						duration: 2000
					});
				  }
				})
			},
			back() {
				this.$router.go(-1)
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.selectedIndexs.length = 0
				this.getData(e.current)
			},
			dialogToggle(type, text) {
				this.msgType = type
				this.messageText = text
				this.$refs.alertDialog.open()
				// 
			},
			dialogClose() {
				console.log('点击关闭')
			},
		}
	}
</script>

<style lang="scss">
	.scorll-style {
		margin-top: 20px;
		width: 100%;
		overflow-x: auto;
	}

	.caozuo {
		display: flex;
	}

	.card-actions {
		padding-bottom: 20upx;
		display: flex;

		.btn1 {
			color: #FFFFFF;
			background-color: #00aa00;
			width: 45%;
			float: left;
		}

		.btn2 {
			color: #FFFFFF;
			background-color: #007AFF;
			width: 90%;
			float: right;
		}
	}
</style>
